/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.gui.facade;

import java.awt.Dimension;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import fr.univ_tln.inf9.exaplanning.api.db.SessionManager;
import fr.univ_tln.inf9.exaplanning.api.facade.event.AdminAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.AdminRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.BuildingAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.BuildingRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ComponentAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ComponentRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListAdminChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListBuildingChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListComponentChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListStudentChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListTeacherChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.StudentAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.StudentRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.TeacherAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.TeacherRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.Student;
import fr.univ_tln.inf9.exaplanning.controleur.facade.UniversityController;
import fr.univ_tln.inf9.exaplanning.controleur.facade.UniversityView;
import fr.univ_tln.inf9.exaplanning.controleur.utilisateur.UserController;

/**
 * @author pivi
 *
 */
public class JFrameListStudent extends UniversityView implements ActionListener  {

	private Student s =null;
	private SessionManager maSession = null;
	UserController ctrl  = null;
	
	private JFrame frame = null;
	private JPanel contentPane = null;
	private JList listStudent = null;
	private JScrollPane scrollStudent = null;
	private DefaultListModel jListModel = null;  //  @jve:decl-index=0:visual-constraint="39,48"
	private static List<String> listzero = null;

	private TextField textField = null;
	
	private JButton addButton;
	private JButton removeButton;
	
	public JFrameListStudent(UniversityController controller) {
		this(controller, listzero);
	}
	
	public JFrameListStudent(UniversityController controller, List<String> a){
		super(controller); 
		buildFrame(a);
	}
	
	private void buildFrame(List<String> r) {
		frame = new JFrame();
		
		contentPane = new JPanel();
		this.contentPane.setSize(20,30);
		
		jListModel = new DefaultListModel();
		
		/**
		 * Insertion des éléments
		 */
		Iterator<String> it = r.iterator(); 
		while(it.hasNext()) {
			jListModel.addElement(it.next());
		}
		
		listStudent = new JList(jListModel);
		
		scrollStudent = new JScrollPane(listStudent);
		contentPane.add(scrollStudent);
		
		this.addButton = new JButton("ajouter étudiant");
		this.addButton.addActionListener(this);
		
		this.removeButton = new JButton("enlever étudiant");
		this.removeButton.addActionListener(this);
		
		textField = new TextField("Votre ajout");
		textField.setSize(new Dimension(50,10));
		//contentPane.add(textField);
		contentPane.add(addButton);
		contentPane.add(removeButton);
		
		
		
		frame.setContentPane(contentPane);
		frame.setTitle("Liste des étudiants :");
		frame.pack();
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("ajouter étudiant")){
			try {
				ctrl = new UserController();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ctrl.displayNewStudent();
		}
		if(arg0.getActionCommand().equals("enlever étudiant")){
		this.getController().notifyStudentRemovedToList((String)this.listStudent.getSelectedValue());
		}
		
	}

	
	
	
	@Override
	public void ListStudentChanged(ListStudentChangedEvent event) {
		jListModel.addElement(event.getNewListStudent());
		
	}

	@Override
	public void StudentAddedToList(StudentAddedToListEvent event) {
		jListModel.addElement(event.getNewStudent());
		
	}

	@Override
	public void StudentRemovedToList(StudentRemovedToListEvent event) {
		jListModel.removeElement(event.getStudentToRemove());
		
	}

	@Override
	public void close() {
		frame.dispose();
	}

	@Override
	public void display() {
		frame.setVisible(true);
	}

	
	/**
	 * Non utilisé
	 */
	
	@Override
	public void ListAdminChanged(ListAdminChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void AdminAddedToList(AdminAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void AdminRemovedToList(AdminRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListBuildingChanged(ListBuildingChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void BuildingAddedToList(BuildingAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void BuildingRemovedToList(BuildingRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListComponentChanged(ListComponentChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ComponentAddedToList(ComponentAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ComponentRemovedToList(ComponentRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListTeacherChanged(ListTeacherChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void TeacherAddedToList(TeacherAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void TeacherRemovedToList(TeacherRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}


}
